//
//  RWPaySuccessViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/14.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWPaySuccessViewController.h"
#import "RWOrderListViewController.h"
#import "RWShoppingCart2ViewController.h"
#import "AppDelegate.h"

@interface RWPaySuccessViewController (){

    NSArray *orderInfos;
    NSArray *orderContents;
}

@end

@implementation RWPaySuccessViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self loadDatas];
    [self setNavigation];
    [self setOrderRemarksUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    orderInfos = @[@"订单编号：", @"成交时间：", @"支付方式：", @"当前状态："];
    NSString *orderId = [NSString stringWithFormat:@"订单编号：%@",_orderId];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
           formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    
    NSString *orderDate = [NSString stringWithFormat:@"成交时间：%@",dateStr];
    if (!_orderType) {
        _orderType = @"余额支付";
    }
    NSString *orderType = [NSString stringWithFormat:@"支付方式：%@",_orderType];
    NSString *orderState = [NSString stringWithFormat:@"当前状态：支付成功"];
    
    orderInfos = @[orderId,orderDate,orderType,orderState];
    
    
}

- (void)loadDatas{
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=addresslist&member_id=%@", [FTYUser sharedUser].userID] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            DLog(@"====%@", result);
            FTYBaseModel *baseModel = [[FTYBaseModel alloc] init];
            baseModel = [FTYBaseModel mj_objectWithKeyValues:result];
            FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
            
            if (0 == errorInfo.errorcode) {
                
           
            } else {
                [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:self.view];
            }
        } else {
            DLog(@"地址获取信息失败：%@", result);
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"支付完成";
    [self.navigationItem setHidesBackButton:YES];
    
}

- (void)setOrderRemarksUI{
    
    _paySuccessTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT- FTY_TABBAR_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_paySuccessTV];
    _paySuccessTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _paySuccessTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _paySuccessTV.dataSource = self;
    _paySuccessTV.delegate = self;
    _paySuccessTV.scrollEnabled = NO;
    _paySuccessTV.tableHeaderView = [self createHeader];
    _paySuccessTV.tableFooterView = [self createFooter];
}

#pragma mark - Config Invoke Methods

- (UIView *)createHeader{
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 100.*FTY_HEIGHT_BASE)];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [headerV addSubview:titleLab];
    titleLab.text = @"成功支付";
    titleLab.textColor = [FTYColor blackColor];
    titleLab.font = FTY_FONTTSIZE(15);
    [titleLab sizeToFit];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25.*FTY_HEIGHT_BASE);
        make.centerX.mas_equalTo(headerV.mas_centerX);
    }];
    
    UILabel *moneyLab = [[UILabel alloc] init];
    [headerV addSubview:moneyLab];
    moneyLab.text = [NSString stringWithFormat:@"¥%@", _payMoney];
    moneyLab.textColor = [FTYColor getMainColorWithAlpha:1.];
    moneyLab.font = FTY_FONTTSIZE(15);
    [moneyLab sizeToFit];
    [moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLab.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.centerX.mas_equalTo(headerV.mas_centerX);
    }];
    
    return headerV;
}

- (UIView *)createFooter{

    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 50.*FTY_HEIGHT_BASE)];
    
    FTYButton *checkOrderBtn = [FTYButton createButtonWithTitle:@"查看订单" tColor:[FTYColor getMainColorWithAlpha:1.] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[UIColor whiteColor]];
    [footerV addSubview:checkOrderBtn];
    [checkOrderBtn addTarget:self action:@selector(checkOrderBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [checkOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH/2);
    }];
    
    //分割线
    UIView *cutLine = [[UIView alloc] init];
    [checkOrderBtn addSubview:cutLine];
    cutLine.backgroundColor = [UIColor grayColor];
    [cutLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo(.5*FTY_WIDTH_BASE);
    }];
    
    FTYButton *goonShopBtn = [FTYButton createButtonWithTitle:@"继续购物" tColor:[FTYColor getMainColorWithAlpha:1.] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[UIColor whiteColor]];
    [footerV addSubview:goonShopBtn];
    [goonShopBtn addTarget:self action:@selector(goonShopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [goonShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH/2);
    }];
    
    //顶部线
    UIView *topLine = [[UIView alloc] init];
    [footerV addSubview:topLine];
    topLine.backgroundColor = [UIColor lightGrayColor];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_WIDTH_BASE);
    }];
    
    //低部线
    UIView *bottomLine = [[UIView alloc] init];
    [footerV addSubview:bottomLine];
    bottomLine.backgroundColor = [UIColor lightGrayColor];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_WIDTH_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notification Method

#pragma mark - Notification Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const paySuccessID = @"paySuccessID";
    UITableViewCell *paySuccessCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:paySuccessID];
    
    NSInteger row = indexPath.row;
    paySuccessCell.selectionStyle = UITableViewCellSelectionStyleNone;
    paySuccessCell.textLabel.text = orderInfos[row];
    paySuccessCell.textLabel.font = FTY_FONTTSIZE(15);
    paySuccessCell.detailTextLabel.text = orderInfos[row];
    paySuccessCell.detailTextLabel.font = FTY_FONTTSIZE(15);
    
    return paySuccessCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - Private Invoke Methods

- (void)checkOrderBtnAction:(UIButton *)btn{

    RWOrderListViewController *orderListVC = [[RWOrderListViewController alloc] init];
    orderListVC.tableTitle = @"全部订单";
    orderListVC.orderState = @"all";
    [self.navigationController pushViewController:orderListVC animated:YES];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"GiftCardActiveSuccessNotification" object:nil];
}

- (void)goonShopBtnAction:(UIButton *)btn{

    //RWShoppingCart2ViewController *shopCartController = [[RWShoppingCart2ViewController alloc] init];
    //[self.navigationController pushViewController:shopCartController animated:YES];
    
    /***
     这个方法容易出现重影的现象 还没有弄清怎么回事
     ***/
    //[self.navigationController popToRootViewControllerAnimated:YES];
    
    //跳转到购物车首页
    
    [sharedDelegate showWindowHome:@"shopCart"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GiftCardActiveSuccessNotification" object:nil];
}

@end
